package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.StringUtils;
import com.zmc.product.infrastructure.service.CommodityCategoryPropertyService;
import com.zmc.product.infrastructure.bean.query.CommodityCategoryPropertyQuery;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.product.infrastructure.dao.CommodityCategoryPropertyDao;
import com.zmc.product.infrastructure.entity.CommodityCategoryPropertyEntity;


/**
 * @author zmc
 */
@Service("commodityCategoryPropertyService")
public class CommodityCategoryPropertyServiceImpl extends ServiceImpl<CommodityCategoryPropertyDao, CommodityCategoryPropertyEntity> implements CommodityCategoryPropertyService {

    @Override
    public PageUtils pageCommodityCategoryPropertyList(CommodityCategoryPropertyQuery query) {
        Page<CommodityCategoryPropertyEntity> page = baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
        return new PageUtils(page);
    }

    @Override
    public List<CommodityCategoryPropertyEntity> selectCommodityCategoryPropertyList(CommodityCategoryPropertyQuery query) {
        return list(getWrapper(query));
    }

    public LambdaQueryWrapper<CommodityCategoryPropertyEntity> getWrapper(CommodityCategoryPropertyQuery query) {
        LambdaQueryWrapper<CommodityCategoryPropertyEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getCategoryId() != null, CommodityCategoryPropertyEntity::getCategoryId, query.getCategoryId())
                .eq(StringUtils.isNotEmpty(query.getPropertyName()), CommodityCategoryPropertyEntity::getPropertyName, query.getPropertyName())
                .eq(StringUtils.isNotEmpty(query.getType()), CommodityCategoryPropertyEntity::getType, query.getType())
                .orderByAsc(CommodityCategoryPropertyEntity::getOrderNum);
        return wrapper;
    }
}